

<?php

    function curPageURL() {
         $pageURL = 'http';
         if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
         $pageURL .= "://";
         if ($_SERVER["SERVER_PORT"] != "80") {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
         } else {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
         }
         return $pageURL;
    }

    function getUserNamePage(){
        if (isset($_SESSION['username']))
	{
		$login = true;
	}
	else{
		$login = false;
		//header("Location: ".$CFG->url."home.php");
	}
	$loguname = $_SESSION['username'];
	$username = $_GET["un"];
	if ($username==NULL&&$login){
		$username=$loguname;
	}
        return $username;
    }

    function getIdThread(){
        $id = getIdThread2();
        if ($id != NULL)
	{
		$ada = true;
	}
	else{
		$ada = false;
	}
	$idthread = $_GET["id"];
	if ($idthread==NULL&&$ada){
		$idthread=$id;
	}
        return $idthread;
    }

    function getIdThread2(){
        $url = curPageURL();
        $k = -1;
        $sleng = strlen($url);
        for ($i = 0; $i < $sleng; ++$i){
            if ($url[$i] == '&'){
                $k = $i + 1;
            }
        }
        if ($k != -1){
            return substr($url, $k);
        }
        else{
            return "";
        }

    }

    function sendpesan(){
        require_once 'php/database.php';
        $idthreadsaatini = getIdThread();
        $user = $_SESSION['username'];

        database::query("INSERT TO message (Pengirim, Pesan, IDThread, TanggalKirim)
            VALUES ('".$user."', '".$_POST[pesanarea]."', '".$idthreadsaatini."', CURRENT_TIMESTAMP )");
        /*mysql_query("INSERT TO message (Pengirim, Pesan, IDThread, TanggalKirim)
            VALUES ('".$user."', '".$_POST[pesanarea]."', '".$idthreadsaatini."', CURRENT_TIMESTAMP )");*/
    }
?>

<?php

    $idthread = getIdThread();
    $user = getUserNamePage();
    $page = $_GET["n"];
    $nperpage = 3;
    
    require_once 'php/database.php';
    require_once 'php/photo.php';

        echo '<br/><br/>';
        echo '<div align="center">';
            echo '<input type="button" id="previsipesan" name="previsipesan" value="prev" onclick="previsipes('.$page.',\''.$user.'\','.$idthread.')">';

            echo '</input>';
        echo '</div>';
        

    $hasil3 = database::query("SELECT * FROM message WHERE IDThread = '$idthread' ORDER BY TanggalKirim DESC");
    if(mysql_num_rows($hasil3)>0){
        $jum = 0;
        
        while ($baris3 = mysql_fetch_row($hasil3)){
            $pengirim = $baris3[0];
            $tanggalkirim = $baris3[3];
            $hasil4 = database::query("SELECT * FROM user WHERE Username = '$pengirim'");
            if(mysql_num_rows($hasil4)>0){
                $baris4 = mysql_fetch_row($hasil4);
                $namapengirim = $baris4[3];

                if($baris4[8] == NULL){
                    $gambarwajah = 'images/bg-photo.png';
                }
                else{
                    $idgambarwajah = $baris4[8];
                    $hasil5 = database::query("SELECT * FROM Foto WHERE IDFOTO = '$idgambarwajah'");
                    if(mysql_num_rows($hasil5)>0){
                        $baris5 = mysql_fetch_row($hasil5);
                        $gambarwajah= 'users/'.$baris5[1].'/'.$baris5[2];
                    }
                }

                $namapengirimArray[$jum] = $namapengirim;
                $gambarwajahArray[$jum] = $gambarwajah;
                $tanggalkirimArray[$jum] = $tanggalkirim;
                $isipesanArray[$jum] = $baris3[1];
                $pengirimArray[$jum] = $pengirim;

                ++$jum;
            }
            //++$jum;
        }

        

        if (($jum % $nperpage) ==  0){
            $ntemp = (($jum - ($jum % $nperpage)) / $nperpage);
        }
        else{
            $ntemp = (($jum - ($jum % $nperpage)) / $nperpage) + 1;
        }
        if ($ntemp > 0){
            $npage = $ntemp - 1;
        }

        for ($j = 0; $j < $nperpage; ++$j){
            $j2 = $page * $nperpage + $j;
            if ($j2 < $jum){
                echo '<div class="box2">';
                    echo '<table align="center">';
                        echo '<tr>';
                            echo '<td>';
                                echo '<img id="gambarpengirim_'.$j2.'" src="'.$gambarwajahArray[$j2].'" border="0" height="75" width="75"/>';
                            echo '</td>';
                            echo '<td>';
                                echo '<div align="center" id="pesanmasuk">';
                                    echo '<div id="tanggal">';
                                        echo '<label>';
                                            echo 'Tanggal Kirim: '.$tanggalkirimArray[$j2];
                                        echo '</label>';
                                    echo '</div>';
                                    echo '<div>';
                                        echo '<textarea id="pesan" disabled="disable" name="pesan">';
                                            echo ''.$isipesanArray[$j2];
                                        echo '</textarea>';
                                    echo '</div>';
                                    echo '<div id="pengirim">';
                                        echo '<a href="profile.php?'.$pengirimArray[$j2].'"><label> '.$namapengirimArray[$j2].' </label></a>';
                                    echo '</div>';
                                echo '</div>';
                            echo '</td>';
                        echo '</tr>';
                    echo '</table>';
                echo '</div>';
            }
        }

    }

    echo '<div align="center">';
        echo '<input type="button" id="nextisipesan" name="nextisipesan" value="next" onclick="nextisipes('.$page.',\''.$user.'\','.$npage.','.$idthread.')">';

        echo '</input>';
    echo '</div>';

?>


